package com.magic.uschat;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.DecimalFormat;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lidroid.xutils.view.annotation.event.OnTouch;
import com.magic.uschat.util.VideoRecoderUtil;
import com.magic.uschat.util.VideoRecoderUtil.RecordTimerCallback;
import com.magic.uschat.widget.PreviewSurface;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.provider.MediaStore.Video.Thumbnails;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

@ContentView(R.layout.activity_short_video_record)
public class ShortVideoActivity extends Activity {

	@ViewInject(R.id.fl_preview)
	private FrameLayout fl_preview;
	@ViewInject(R.id.tv_cancle)
	private TextView tv_cancle;
	@ViewInject(R.id.iv_switch_camera)
	private ImageView iv_switch_camera;
	
	@ViewInject(R.id.pb_record)
	private ProgressBar pb_record;
	@ViewInject(R.id.iv_short_play)
	private ImageView iv_short_play;
	@ViewInject(R.id.iv_short_sure)
	private ImageView iv_short_sure;
	
	@ViewInject(R.id.btn_record)
	private Button btn_record;
	
	@ViewInject(R.id.iv_record_identifi)
	private ImageView iv_record_identifi;
	@ViewInject(R.id.tv_record_time)
	private TextView tv_record_time;
	
	@ViewInject(R.id.iv_switch_flash)
	private ImageView iv_switch_flash;
	
	private VideoRecoderUtil videoRecordUtil;
	private AlphaAnimation recordAnima;
	
	private File videoFile;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		ViewUtils.inject(this);
		init();
	}
	
	public void init(){
		videoRecordUtil = VideoRecoderUtil.getInstance(this);
		PreviewSurface sv = videoRecordUtil.obtainSurface(this);
		fl_preview.addView(sv,0);
		//videoRecordUtil.startRecorder();
		pb_record.setMax(Config.MAX_RECORD_TIME);
		recordAnima = (AlphaAnimation) AnimationUtils.loadAnimation(this, R.drawable.record_pointer_anima);
		recordAnima.setRepeatMode(Animation.REVERSE);
		recordAnima.setRepeatCount(-1);
		showOptionBtn(false);
	}
	
	@OnClick(R.id.tv_cancle)
	public void onCancle(View v){
		this.finish();
	}
	@OnClick(R.id.iv_switch_camera)
	public void onSwitchCameraClkick(View v){
		videoRecordUtil.swithcCamera();
	}
	@OnClick(R.id.iv_switch_flash)
	public void onSwitchFlash(View v){
		videoRecordUtil.switchFlash();
	}
	
	
	private void createVideoThumb(File f){
		Bitmap b = ThumbnailUtils.createVideoThumbnail(f.getAbsolutePath(),Thumbnails.MICRO_KIND);
		Bitmap bmap = ThumbnailUtils.extractThumbnail(b, 480, 320,ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
		File fmap = new File(f.getParentFile().getAbsolutePath(),f.getName().substring(0, f.getName().lastIndexOf("."))+".jpg");
		FileOutputStream fos=null;
		try {
			fos = new FileOutputStream(fmap);
			bmap.compress(CompressFormat.JPEG,100, fos);
			fos.flush();
			fos.close();
		} catch (Exception e) {
			e.printStackTrace();
			Log.e("videothumb",e.getLocalizedMessage());
		}
		
	}
	
	@OnTouch(R.id.btn_record)
	private boolean btnRecordTouch(View v,MotionEvent event){
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			btn_record.setText("");
			videoRecordUtil.startRecorder(new RecordListener());
			iv_record_identifi.setImageResource(R.drawable.red_pointer);
			iv_record_identifi.startAnimation(recordAnima);
			break;
		case MotionEvent.ACTION_UP:
			btn_record.setText(getString(R.string.press_recorder));
			videoRecordUtil.stopRecorder();
			break;
		}
		return false;
	}
	
	class RecordListener implements RecordTimerCallback{

		@Override
		public void onRecording(int time) {
			pb_record.setProgress(Config.MAX_RECORD_TIME-time);
			DecimalFormat df = new DecimalFormat("0.00");
			double d = (double)((Config.MAX_RECORD_TIME-time)/1000f);
			duration = Double.parseDouble(df.format(d));
			tv_record_time.setText(df.format(d)+"''");
		}

		@Override
		public void onRecordFinish(File f) {
			iv_record_identifi.clearAnimation();
			iv_record_identifi.setImageResource(R.drawable.white_pointer);
			showOptionBtn(true);
			videoFile = f;
		}

		@Override
		public void onUp(File f) {
			iv_record_identifi.clearAnimation();
			iv_record_identifi.setImageResource(R.drawable.white_pointer);
			showOptionBtn(true);
			videoFile = f;
		}
		
	}
	
	private double duration=0;
	
	@OnClick(R.id.iv_short_sure)
	private void selectVideoClick(View v){
		createVideoThumb(videoFile);
		Intent data = new Intent();
		data.putExtra("video",videoFile);
		data.putExtra("duration",duration);
		setResult(RESULT_OK, data);
		this.finish();
	}
	
	private void showOptionBtn(boolean flag){
		if (flag) {
			iv_short_play.setVisibility(View.VISIBLE);
			iv_short_sure.setVisibility(View.VISIBLE);
		}else{
			iv_short_play.setVisibility(View.GONE);
			iv_short_sure.setVisibility(View.GONE);
		}
		iv_switch_camera.setEnabled(!flag);
		btn_record.setEnabled(!flag);
	}
	
	@Override
	protected void onStart() {
		super.onStart();
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		videoRecordUtil.releaseMedia();
	}
	
}
